Micron Document
Livres et Wikis | Archives | Info


JavaScript syntax
part 11/30 Β· 107.0 KB total
layout: Wide Β· Narrow Β· Centered
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
x[Symbol.iterator] === Array.prototype[Symbol.iterator]; // and Arrays
are iterable
const xIterator = x[Symbol.iterator](); // The [Symbol.iterator]
function should provide an iterator for x
xIterator.next(); // { value: 1, done: false }
xIterator.next(); // { value: 2, done: false }
xIterator.next(); // { value: 3, done: false }
xIterator.next(); // { value: 4, done: false }
xIterator.next(); // { value: undefined, done: true }
xIterator.next(); // { value: undefined, done: true }
// for..of loops automatically iterate values
for (const value of x) {
console.log(value); // 1 2 3 4
}
// Sets are also iterable:
[Symbol.iterator] in Set.prototype; // true
for (const value of new Set(['apple', 'orange'])) {
console.log(value); // "apple" "orange"
}

Native objects

The JavaScript language provides a handful of native objects. JavaScript
native objects are considered part of the JavaScript specification.
JavaScript environment notwithstanding, this set of objects should
always be available.

Array

An Array is a JavaScript object prototyped from the Array constructor
specifically designed to store data values indexed by integer keys.
Arrays, unlike the basic Object type, are prototyped with methods and
properties to aid the programmer in routine tasks (for example, join,
slice, and push).

As in the C family, arrays use a zero-based indexing scheme: A value
that is inserted into an empty array by means of the push method
occupies the 0th index of the array.

const myArray = []; // Point the variable myArray to a newly ...
// ... created, empty Array
myArray.push("hello World"); // Fill the next empty index, in this case
0
console.log(myArray[0]); // Equivalent to console.log("hello World");

Arrays have a length property that is guaranteed to always be larger
than the largest integer index used in the array. It is automatically
updated, if one creates a property with an even larger index. Writing a
smaller number to the length property will remove larger indices.

Elements of Arrays may be accessed using normal object property access
notation:

myArray[1]; // the 2nd item in myArray
myArray["1"];

The above two are equivalent. It is not possible to use the
"dot"-notation or strings with alternative representations of the
number:

myArray.1; // syntax error
myArray["01"]; // not the same as myArray[1]

Declaration of an array can use either an Array literal or the Array
constructor:

let myArray;
// Array literals
myArray = [1, 2]; // length of 2
myArray = [1, 2,]; // same array - Users can also have an extra comma at
the end
// It is also possible to not fill in parts of the array
myArray = [0, 1, /* hole */, /* hole */, 4, 5]; // length of 6
myArray = [0, 1, /* hole */, /* hole */, 4, 5,]; // same array
myArray = [0, 1, /* hole */, /* hole */, 4, 5, /* hole */,]; // length
of 7
// With the constructor
myArray = new Array(0, 1, 2, 3, 4, 5); // length of 6
myArray = new Array(365); // an empty array with length 365


──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────